"use server"

import bcrypt from 'bcrypt'
import { prisma } from '@/lib/db'
import { RegisterFormSchemaType } from './page'

export const register = async (values: RegisterFormSchemaType): Promise<void | { error?: string }> => {
  const { username, email, password } = values
  const exitUser = await prisma.user.findUnique({
    where: {
      email: email
    }
  })

  if (exitUser) {
    return {
      error: '当前邮箱已存在！'
    }
  }

  const hashedPassword = await bcrypt.hash(password, 10)
  await prisma.user.create({
    data: {
      name: username,
      password: hashedPassword,
      email: email
    }
  })
}